import logging
import sys

from config.const import Const


def get_logger(name=Const.CI_APP_NAME, logger_level=logging.INFO):
    handler = logging.StreamHandler(sys.stderr)

    if Const.ENV not in ('prod', 'localprod'):
        formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s')
    else:
        formatter = logging.Formatter(
            '{{"env": "{env}", "@timestamp": "%(asctime)s", "level": "%(levelname)s", "channel": "{channel}",'
            ' "type": "{type}", "module": "%(module)s", "message": "%(message)s"}}'.format(
                env=Const.ENV, type=Const.CI_APP_NAME, channel=Const.CI_PROJECT_NAMESPACE),
            datefmt='%Y-%m-%dT%H:%M:%S.%f')

    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.addHandler(handler)
    logger.setLevel(logger_level)
    return logger
